package servlet.xml.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet");
        PrintWriter writer = resp.getWriter();
        writer.print("HelloServlet");
        // writer.close();
        throw new ServletException("hahah");
    }

    @Override
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doHead(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Session 读取
        HttpSession session = req.getSession(true);
        String ip = (String) session.getAttribute("ip");
        String str = (String) session.getAttribute("jhxxb");
        resp.getWriter().println("SessionID=" + session.getId() + ",ip=" + ip + "," + str);
        // session.invalidate(); // session 销毁

        // Cookie 读取
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            resp.getWriter().println(cookie.getName() + ":" + cookie.getValue() + ":" + cookie.getDomain() + ":" + cookie.getPath() + ":" + cookie.getMaxAge());
            // Cookie 删除
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Session 写入
        HttpSession session = req.getSession(true);
        session.setAttribute("ip", req.getRemoteAddr());
        session.setAttribute("jhxxb", "very good!");
        resp.getWriter().println("SessionID=" + session.getId() + ",Set Session OK!");

        // Cookie 写入
        Cookie cookie = new Cookie("name", "value");
        cookie.setMaxAge(60 * 60 * 24 * 7);
        resp.addCookie(cookie);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }

    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doOptions(req, resp);
    }

    @Override
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doTrace(req, resp);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        super.service(req, res);
    }
}